package com.xckj.cycling.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.text.Editable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;

import androidx.annotation.NonNull;

import com.xckj.cycling.R;
import com.xckj.cycling.bean.UserinfoBean;
import com.xckj.cycling.databinding.ActivityRegisterBinding;
import com.xckj.cycling.framework.BaseActivity;
import com.xckj.cycling.global.Constant;
import com.xckj.cycling.iview.IRegisterView;
import com.xckj.cycling.iview.ISendCodeView;
import com.xckj.cycling.presenter.RegisterPresenter;
import com.xckj.cycling.presenter.SendCodePresenter;
import com.xckj.cycling.utils.AndroidUtil;
import com.xckj.cycling.utils.CheckFormatUtil;
import com.xckj.cycling.utils.SpUtil;

import java.util.Objects;

/**
 * @Author: ChenYao
 * @Describe: 注册
 * @Date: 2023/3/1 15:20
 */
public class RegisterActivity extends BaseActivity<ActivityRegisterBinding> implements IRegisterView, ISendCodeView {
    private RegisterPresenter registerPresenter;
    private SendCodePresenter sendCodePresenter;


    @Override
    protected int loadLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void setTitle() {
        super.setTitle();
        dataBinding.viewTitle.tvTitle.setText(getString(R.string.register));
        dataBinding.viewTitle.tvRight.setText(getString(R.string.login));
    }

    @Override
    protected void initData() {
        registerPresenter = new RegisterPresenter(this);
        sendCodePresenter = new SendCodePresenter(this);

        String tips1 = getString(R.string.privacy_1);
        String tips2 = getString(R.string.privacy_2);
        String tips = getString(R.string.privacy_tips, tips1, tips2);
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(tips);
        //        stringBuilder.setSpan(new ForegroundColorSpan(color),index ,index+tips1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        int color = Color.parseColor("#0AD39F");
        int index = tips.indexOf(tips1);
        stringBuilder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                BrowserActivity.start(RegisterActivity.this, "yinsixieyi");
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(color);
            }
        }, index, index + tips1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        index = tips.indexOf(tips2);
        stringBuilder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                BrowserActivity.start(RegisterActivity.this, "userxieyi");
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(color);
            }
        }, index, index + tips2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        dataBinding.privacy.setText(stringBuilder);
        dataBinding.privacy.setMovementMethod(LinkMovementMethod.getInstance());
        dataBinding.privacy.setOnClickListener(onClickListener);
        dataBinding.ivPrivacy.setOnClickListener(onClickListener);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean select = dataBinding.ivPrivacy.isSelected();
            dataBinding.ivPrivacy.setSelected(!select);
        }
    };

    @Override
    protected void setListener() {
        dataBinding.etConfirmPassword.addTextChangedListener(textWatcher);
        dataBinding.etMailbox.addTextChangedListener(textWatcher);
        dataBinding.etPassword.addTextChangedListener(textWatcher);
        dataBinding.etVerificationCode.addTextChangedListener(textWatcher);
        dataBinding.viewTitle.tvRight.setOnClickListener(this);
        dataBinding.cvRegisterCountdown.setOnClickListener(this);
        dataBinding.btnRegister.setOnClickListener(this);
        dataBinding.btnFacebookLogin.setOnClickListener(this);
        dataBinding.btnGoogleLogin.setOnClickListener(this);
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            checkButton();
        }
    };

    private void checkButton() {
        boolean b = isNotEmpty(dataBinding.etConfirmPassword) && isNotEmpty(dataBinding.etMailbox) && isNotEmpty(dataBinding.etPassword)
                && isNotEmpty(dataBinding.etVerificationCode);
        dataBinding.btnRegister.setEnabled(b);
    }


    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        String mailbox = dataBinding.etMailbox.getText().toString();
        String code = dataBinding.etVerificationCode.getText().toString();
        String password = Objects.requireNonNull(dataBinding.etPassword.getText()).toString();
        String confirmPassword = Objects.requireNonNull(dataBinding.etConfirmPassword.getText()).toString();
        if (AndroidUtil.isFastDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_right:
                login();
                break;
            case R.id.cv_register_countdown:
                sendCode(mailbox);
                break;
            case R.id.btn_register:
                register(mailbox, code, password, confirmPassword);
                break;
            case R.id.btn_facebook_login:
                facebookLogin();
                break;
            case R.id.btn_google_login:
                googleLogin();
                break;
            default:
                break;
        }
    }

    private void login() {
        finish();
    }

    private void sendCode(String mailbox) {
        if (TextUtils.isEmpty(mailbox)) {
            showMessage(getString(R.string.mailbox_hint));
            return;
        }
        if (!CheckFormatUtil.isMailbox(mailbox)) {
            showMessage(getString(R.string.mailbox_error));
            return;
        }
        sendCodePresenter.sendCode("register", mailbox);
    }

    private void register(String mailbox, String code, String password, String confirmPassword) {
        boolean select = dataBinding.ivPrivacy.isSelected();
        if (!select) {
            showMessage(getString(R.string.privacy_agree, getString(R.string.privacy_1), getString(R.string.privacy_2)));
            return;
        }
        if (TextUtils.isEmpty(mailbox)) {
            showMessage(getString(R.string.mailbox_hint));
            return;
        }
        if (!CheckFormatUtil.isMailbox(mailbox)) {
            showMessage(getString(R.string.mailbox_error));
            return;
        }
        if (TextUtils.isEmpty(code)) {
            showMessage(getString(R.string.code_null_hint));
            return;
        }
        if (TextUtils.isEmpty(password)) {
            showMessage(getString(R.string.mailbox_hint));
            return;
        }
        if (password.length() < Constant.PASSWORD_MIN_LENGTH) {
            showMessage(getString(R.string.password_length_error));
            return;
        }
        if (TextUtils.isEmpty(confirmPassword)) {
            showMessage(getString(R.string.password_null_hint));
            return;
        }
        if (confirmPassword.length() < Constant.PASSWORD_MIN_LENGTH) {
            showMessage(getString(R.string.password_length_error));
            return;
        }
        if (!password.equals(confirmPassword)) {
            showMessage(getString(R.string.password_compare));
            return;
        }
        try {
            int i = Integer.parseInt(password);
            showMessage(getString(R.string.password_compare1));
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        goToRegister(mailbox, code, password);
    }

    private void goToRegister(String mailbox, String code, String password) {
        registerPresenter.register(mailbox, code, password);
    }

    private void facebookLogin() {
    }

    private void googleLogin() {
    }

    @Override
    public void success(UserinfoBean userinfo) {
        SpUtil.put(this, Constant.TOKEN, userinfo.getToken());
        startActivity(new Intent(this, PerfectRegisterActivity.class));
        finish();
    }

    @Override
    public void failure(String message) {
        showMessage(message);
    }

    @Override
    public void sendCodeSuccess(String message) {
        showMessage(getString(R.string.send_code_hint));
        dataBinding.cvRegisterCountdown.start();
    }

    @Override
    public void sendCodeFailure(String message) {
        showMessage(message);
    }
}
